package zb.demo.controller;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;
import zb.demo.service.IFileInputUploadService;

import javax.servlet.http.HttpServletRequest;
import java.util.List;
import java.util.Map;

/**
 * @Author: BinZhang
 * @Date: 2018/6/11 16:36
 * @Description:fileinput实现文件上传
 */
@Controller
@RequestMapping("/demo/fileInputUpload")
public class FileInputUploadController {
    @Autowired
    private IFileInputUploadService iFileInputUploadService;

    @RequestMapping(value = "/upload", method = RequestMethod.POST, produces = "text/json;charset=UTF-8")
    @ResponseBody
    public String fileUpload(@RequestParam(value = "files") MultipartFile files, String id, HttpServletRequest request) throws JsonProcessingException {
        return new ObjectMapper().writeValueAsString(iFileInputUploadService.fileUpload(files, request));
    }

    @RequestMapping(value = "/getUploadFile", method = RequestMethod.POST)
    @ResponseBody
    public List<Map<String, String>> getUploadFile(HttpServletRequest request) {
        return iFileInputUploadService.getUploadFile(request);
    }

}
